Ext.extend(app.module,{
    currentFormValues: {},
    init: function(){
        mainPanel = this;
        this.jtyszjzdDs = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'project/listForJtyszjzd.do'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'description'},
                {name:'projecttype'},
                {name:'state'},
                {name:'reporttime'},
                {name:'approvaltime'},
                {name:'planendtime'},
                {name:'endtime'},
                {name:'tasktime'},
                {name:'middletime'},
                {name:'finishtime'},
                {name:'filepath'},
                {name:'taskpath'},
                {name:'remain'},
                {name:'filename'},
                {name:'projectTypeName'},
                {name:'iscross'},
                {name:'approvedept'},
                {name:'rank'},
                {name:'shoolmoney'},
                {name:'remarks'},
                {name:'deptname'},
                {name:'names'}
            ])
        });

        var jtyszjzd_pagesize_combo = new Ext.form.ComboBox( {       //设置分页栏的每页记录数选择
            name : 'pagesize',
            hiddenName : 'pagesize',
            typeAhead : true,
            triggerAction : 'all',
            lazyRender : true,
            mode : 'local',
            store : new Ext.data.ArrayStore(
                {
                    fields : [ 'value', 'text' ],
                    data : [ [ 20, '20条/页' ],[ 50, '50条/页' ], [ 100, '100条/页' ],[ 200, '250条/页' ]]
                }),
            valueField : 'value',
            displayField : 'text',
            value : '20',
            editable : false,
            width : 85
        });
        var number = parseInt(jtyszjzd_pagesize_combo.getValue());

        jtyszjzd_pagesize_combo.on("select", function(comboBox) {
            var bbar = Ext.getCmp('jtyszjzdbbar');
            bbar.pageSize = parseInt(comboBox.getValue());
            number = parseInt(comboBox.getValue());
            mainPanel.jtyszjzdDs.reload( {
                params : {
                    start : 0,
                    limit : bbar.pageSize
                }
            });
        });
        this.body = new Ext.grid.GridPanel({
            store: this.jtyszjzdDs,
            region: 'center',
            loadMask: true,
            viewConfig:{
                forceFit:true
            },
            columns: [
                new Ext.grid.RowNumberer(),
                {
                    xtype: 'numbercolumn',
                    dataIndex: 'id',
                    header: 'id',
                    hidden: true
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'name',
                    header: '点击课题名称查看',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        return '<a href="javascript:void(0)" onclick="mainPanel.getFileViewDlg('+
                            record.data["id"]+')">'+value+'</a>';
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'approvedept',
                    header: '来源部门'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'projectTypeName',
                    header: '课题类型'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'names',
                    header: '负责人',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if(value.indexOf(",")==-1){
                            return value;
                        } else {
                            return value.substring(0,value.indexOf(","));
                        }
                    }
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'deptname',
                    header: '负责人部门'
                },
                {
                    xtype: 'gridcolumn',
                    align: 'center',
                    dataIndex: 'state',
                    header: '状态',
                    renderer:function(value, cellmeta, record, rowIndex, columnIndex, store){
                        if(value==8) return '提交申请';
                        else if(value==9) return '已指派专家';
                        else return '';
                    }
                },
                {
                    xtype: 'actioncolumn',
                    width: 50,
                    header: '指派专家',
                    align: 'center',
                    items: [
                        {
                            icon: '../images/ext/config.png',
                            tooltip: '指派专家审核课题',
                            handler: function(grid, rowIndex, colIndex) {
                                var record = grid.getStore().getAt(rowIndex);
                                mainPanel.dlg = mainPanel.getJtyssjzdDialog(record.data.id);
                                mainPanel.dlg.setTitle("为课题："+record.data.name+" 指派专家");
                                mainPanel.dlg.show();
                            }
                        }
                    ]
                }
            ],
            bbar: {
                id: 'jtyszjzdbbar',
                xtype: 'paging',
                pageSize : number,
                store : this.jtyszjzdDs,
                displayInfo : true,
                displayMsg : '显示{0}条到{1}条,共{2}条',
                emptyMsg : "没有符合条件的记录",
                items : [ '-', '&nbsp;&nbsp;', jtyszjzd_pagesize_combo ]
            }
        });
        this.main.add(this.body);
        this.main.doLayout();
        this.jtyszjzdDs.load({
            params: {start:0, limit:20 }
        });
    },
    getJtyssjzdDialog: function(projectId){
        mainPanel = this;
        var comboSubject = new Ext.form.ComboBox({
            store: new Ext.data.Store({
                proxy: new Ext.data.HttpProxy({url:'subject/list.do'}),
                reader: new Ext.data.JsonReader({
                    root: 'rows',
                    idProperty: 'id'
                },[
                    {name:'id'},
                    {name:'name'}
                ])
            }),
            region:'north',
            editable: false,
            emptyText: '请选择学科组...',
            mode: 'remote',
            hiddenName : 'titleid',
            typeAhead : true,
            triggerAction : 'all',
            displayField: 'name',
            valueField: 'id',
            fieldLabel: '学科组',
            listeners:{
                "select":function(combo,record,number){
                    expertDs.load({params:{subjectid:combo.getValue()}});
                },
                scope:this
            }
        });
        var expertDs = new Ext.data.Store({
            proxy: new Ext.data.HttpProxy({url:'user/list.do?roleid=2'}),
            reader: new Ext.data.JsonReader({
                totalProperty: 'total',
                root: 'rows',
                idProperty: 'id'
            },[
                {name:'id'},
                {name:'name'},
                {name:'username'},
                {name:'deptid'},
                {name:'deptname'},
                {name:'job'},
                {name:'telephone'},
                {name:'titleid'},
                {name:'titlename'},
                {name:'roleIds'},
                {name:'roleNames'}
            ])
        });
        expertDs.load();
        this.jtyssjzdForm = new Ext.form.FormPanel({
            region: 'center',
            labelAlign: 'right',
            bodyStyle: 'padding:5px',
            buttonAlign: 'center',
            border:false,
            msgTarget: 'side',
            labelWidth: 10,
            bodyStyle: 'padding:10px;',
            items: [
                {
                    xtype: 'itemselector',
                    name: 'itemselector',
                    imagePath: 'js/ux/images/',
                    region: 'center',
                    multiselects: [{
                        width: 200,
                        height: 300,
                        store: expertDs,
                        displayField: 'username',
                        valueField: 'id',
                        legend: '专家列表'
                    },{
                        width: 200,
                        height: 300,
                        legend: '已选专家',
                        tbar : [{
                            text : '清除',
                            handler : function() {
                                mainPanel.jtyssjzdForm.getForm().reset();
                            }
                        }],
                        //纠结的问题，这儿得写成store: [[]]，而不是store: []，否则取不到值
                        store: [[]]
                    }]
                }
            ],
            buttons: [{
                text: '保存',
                scope: this,
                handler: function() {
                    this.submit(projectId);
                }
            }]
        });
        var dlg = new Ext.Window({
            height: 410,
            width: 475,
            layout: 'border',
            border: false,
            title: '指派专家',
            modal: true,
            plain: true,
            layout: 'border',
            resizable: false,
            items: [
                {
                    xtype: 'form',
                    region:'north',
                    bodyStyle: 'padding:5px',
                    labelAlign: 'right',
                    labelWidth:60,
                    border:false,
                    height:30,
                    items: [
                        comboSubject
                    ]
                },
                this.jtyssjzdForm
            ],
            listeners: {
                scope: this,
                render: function(fp){
                    this.jtyssjzdForm.getForm().waitMsgTarget = fp.getEl();
                },
                show: function(){
                    var f = this.jtyssjzdForm.getForm();
                    f.setValues(this.currentFormValues);
                    f.clearInvalid();
                }
            }
        });
        return dlg;
    },
    submit: function(projectId){
        mainPanel = this;
        if(this.jtyssjzdForm.getForm().isValid()){
            var ids = this.jtyssjzdForm.getForm().findField('itemselector').getValue();
            if(ids.length<=1){
                Ext.Msg.alert('提示','你还啥都没选呢！');
            } else {
                Ext.Ajax.request({
                    method:'post',
                    url:'jtpsyj/appointExperts.do',
                    params:{
                        //去掉最前边那个逗号
                        expertids:ids.substr(1,ids.length-1),
                        projectid:projectId
                    },
                    success: function(resp, opts) {
                        var result = Ext.util.JSON.decode(resp.responseText);
                        var info = result.info;
                        if(result.success=='true') {
                            mainPanel.jtyszjzdDs.reload();
                            mainPanel.dlg.hide();
                            Ext.Msg.alert('信息', info);
                        }else {
                            Ext.Msg.alert('信息', info);
                        }
                    },
                    failure: function(resp,opts) {
                        var result = Ext.util.JSON.decode(resp.responseText);
                        var info = result.info;
                        Ext.Msg.alert('提示', info);
                    }
                });
            }
        }
    }
});
